<?php

namespace app\common\job;

use app\common\library\Jcc;
use app\common\model\game\Groups;
use app\common\model\game\Record;
use app\common\model\room\Lists;
use fast\Http;
use think\Cache;
use think\Db;
use think\queue\Job;

class Room
{

    //$room_info['data']['status'];//房间状态
    //$room_info['data']['teamInfo']['members'];//房间内数组
    //$room_info['data']['teamInfo']['members']['roleId'];//游戏ID
    public function query(Job $job, $data)
    {
        $model = new Groups();
        $groups = $model->where(['third_id' => $data['fromgid']])->find();
        $date = date("Y-m-d H:i:s");
        try {
            if ($groups) {//群聊房间匹配
                $session_id = Jcc::session_id(false);
                $json = json_encode([
                    "teamId" => $data['teamId'],
                    "session_id" => $session_id
                ]);
                $third_url = Db::name('config')->where(['name' => 'third_url'])->value("value", "");
                $third_url_array = explode(",", $third_url);
                $room_result = Http::post($third_url_array[0] . "GetTeamData", $json);//查询房间信息
                $room_info = json_decode($room_result, true);

                if (isset($room_info['ret']) && $room_info['ret'] == 0) {//房间信息查询校检

                    $room = Lists::where(["team_id" => $data['teamId'], 'status' => 3])->find();
                    if ($room) {
                        echo $date . "房间-已解散,房间ID" . $data['teamId'];
                        $job->delete();
                        return true;
                    }

                    if ($room_info['data']['status'] == 1) {//未开始

                        $room_change = Lists::change($room_info['data']['teamInfo']['members'], $groups, $data['teamId'], $data['fromgid'], $data['uin'], $data['fromname'] ?? 0);

                        if ($room_change['code'] == 1) {
                            echo $date . "房间ID:" . $data['teamId'];
                            $job->release(3);//重复监听
                        } else {
                            echo $date . $room_change['msg'] . "房间ID:" . $data['teamId'];
                            $job->delete();
                        }

                    } elseif ($room_info['data']['status'] == 2) {//已解散

                        $dissolve = Lists::dissolve($data['teamId']);
                        if ($dissolve['code'] == 1) {
                            $msg = "当前房间已解散";
                        } else {
                            $msg = $dissolve['msg'];
                        }

                        echo $date . $msg . ",房间ID:" . $data['teamId'];
                        $job->delete();
                    } elseif ($room_info['data']['status'] == 3) {

                        echo $date . "寻找对局中,房间ID:" . $data['teamId'];
                        $job->release(3);//重复监听
                    } elseif ($room_info['data']['status'] == 4) {//战斗中
                        $room = Db::name("room_lists")
                            ->where("team_id", $data['teamId'])
                            ->where('status', 0)->find();

                        if ($room) {
                            Db::name("room_lists")
                                ->where("team_id", $data['teamId'])
                                ->update(['status' => 1]);
                        }

                        $change = Lists::change($room_info['data']['teamInfo']['members'], $groups, $data['teamId'], $data['fromgid'], $data['uin'], $data['fromname'] ?? 0);

                        if ($change['code'] == 1) {
                            echo $date . "战斗进行中.时间拉长.房间ID:" . $data['teamId'];
                        } else {
                            echo $date . '战斗中-' . $change['msg'] . ".房间ID:" . $data['teamId'];
                        }

                        $job->release(60);//重复监听
                    } elseif ($room_info['data']['status'] == 5) {

                        $record = Record::settle($room_info['data']['teamInfo']['members'], $data['teamId'], $groups);
                        if ($record['code'] == 1) {
                            $msg = "战绩已结算";
                        } else {
                            $msg = $record['msg'];
                        }
                        echo $date . $msg . "-战绩结束.房间ID:" . $data['teamId'];
                        $job->delete();
                    } else {
                        if (!empty($room_result)) {
                            Db::name('third_notinfo')->insert([
                                'formg_id' => 0,
                                'team_id' => $data['teamId'],
                                'info' => $room_result,
                                'createtime' => date("Y-m-d H:i:s")
                            ]);
                        }
                        echo $date . "无.直接结束.房间ID:" . $data['teamId'];
                        $job->delete();
                    }
                } else {
                    if (!empty($room_result)) {
                        Db::name('third_info')->insert([
                            'info' => $room_result,
                            'type' => 1,
                            'createtime' => date("Y-m-d H:i:s")
                        ]);
                    }

                    if (Cache::has("room_id_" . $data['teamId'])) {//已经缓存
                        $refresh_num = Cache::get("room_id_" . $data['teamId']);
                        if ($refresh_num >= 4) {
                            echo $date . "当前房间信息拉取" . $refresh_num . "次失败,结束,房间ID:" . $data['teamId'];
                            $job->delete();
                        } else {
                            echo $date . "当前房间信息拉取失败,第" . $refresh_num . ",房间ID:" . $data['teamId'];
                            Cache::set("room_id_" . $data['teamId'], $refresh_num + 1, 86400);
                            $job->release(5);
                        }
                    } else {
                        Cache::set("room_id_" . $data['teamId'], 1, 86400);
                        Jcc::session_id(true);
                        echo $date . "当前房间信息拉取失败,更换URL,房间ID:" . $data['teamId'];
                        $job->release(5);
                    }
                }
            } else {
                echo $date . "群聊房间不存在,群聊ID:" . $data['fromgid'];
                $job->delete();
            }
        } catch (\Exception $e) {
            var_dump($e->getTrace());
            echo $date . $e->getMessage();
            $job->delete();
        }
    }
}